#include <iostream>
#include"head.h"
using namespace std;

class heap{
private:
    int num[10]={10,3,14,2,6,12,16,4,7,15};
    int currentSize=10;
public:
    heap();
    void BuildHeap();
    void SiftDown(int left);
    void deleteEle(int target);
    void show();
};

void heap::show(){
    for(int i = 0;i<currentSize;i++)
        cout<<num[i]<<"  ";
}


heap::heap(){
    for(int i=0;i<10;i++)
        cout<<num[i]<<"\t";
    cout<<endl;
    BuildHeap();
    for(int i=0;i<10;i++)
        cout<<num[i]<<"\t";
    cout<<endl;
}


void heap::BuildHeap(){
    for(int i=currentSize/2-1;i>=0;i--)
        SiftDown(i);
}

void heap::SiftDown(int left){
    int i = left;
    int j = 2*i + 1;
    int temp=num[i];

    while(j<currentSize){
        if((j<currentSize-1)&&(num[j]<num[j+1]))
            j++;
        if(temp<num[j])
        {
            num[i]=num[j];
            i=j;
            j=2*j+1;
        }
        else break;
    }
    num[i]=temp;
}

void heap::deleteEle(int target){
    int i;
    for(i=0;i<currentSize;i++)
    {
        if(num[i]==target)
            break;
    }
    num[i]=num[currentSize-1];
    currentSize--;
//    SiftDown(i);
    BuildHeap();
    show();
}


int main()
{
    heap h;
    int num;
    while(1){
        cout<<endl;
        cout<<"delete element:  ";
        cin>>num;
        h.deleteEle(num);
    }
}
